# ruby exercise - advanced
# mario hsieh


class MathDojo
	@sum

	def initialize
		@sum = 0
	end

	def add(num1, num2 = 0)
		@sum += num1 + num2
		return self
	end

	def subtract(num1, num2 = 0)
		@sum -= num1 + num2
		return self
	end
	
	def display
		puts @sum
	end
end

test = MathDojo.new()

#MathDojo.new.add(2).add(2,5).subtract(3,2).display


#########################################################


class MathDojo2

	# declare variables
	@sum
	@temp

	def initialize
		@sum = 0
	end

	# add function
	def add(num1, *nums)
		# first argument
		if num1.kind_of?(Array)
			num1.each do |n|
				@sum += n
			end
		elsif num1.kind_of?(Integer)
			@sum += num1			
		end

		# *nums arguments
			nums.each do |x|
			if x.kind_of?(Array)
				x.each do |y|
					@sum += y
				end
			elsif x.kind_of?(Integer)
				@sum += x
			end
		end		

		return self
	end	

	# subtract function
	def subtract(num1, *nums)
		# first argument
		if num1.kind_of?(Array)
			@temp = 0
			num1.each do |n|
				@temp += n
			end
			@sum -= @temp
		elsif num1.kind_of?(Integer)
			@sum -= num1
		end 

		# other arguments
		nums.each do |x|
			if x.kind_of?(Array)
				@temp = 0
				x.each do |y|
					@temp += y
				end
				@sum -= @temp
			elsif x.kind_of?(Integer)
				@sum -= x
			end
		end		
		return self
	end

	# display function
	def display
		puts @sum
	end

end

#MathDojo2.new.add(1).add([3,5,7,8],[2,4.3,1.25]).subtract([2,3],[1.1,2.3]).display

MathDojo2.new.add(1).add([2,3,9]).add([4,8,1], 3, [2,5]).subtract(3).subtract([7,6,4]).subtract(1, [1], [1,2,3]).display







